#!/bin/bash
# Use this script to update all the .ll files from the .c and .cpp files

clang="clang"
ikos_pp="ikos-pp"
opt="opt"
opt_level="aggressive"
cflags="-c -emit-llvm -Wall -D_FORTIFY_SOURCE=0 -D__IKOS__ -g -O0 -Xclang -disable-O0-optnone"
cxxflags="$cflags -std=c++17"
ikos_import="ikos-import"
ikos_import_opts="-format=text -order-globals -allow-dbg-mismatch"

set -e

function run {
    echo "#" "$@"
    "$@"
}

for filename in *.c
do
    echo "[*] $filename ..."
    filename="${filename%.c}"

    run "$clang" $cflags "$filename.c" -o "$filename.bc"
    run "$ikos_pp" -opt=$opt_level "$filename.bc" -o "$filename.pp.bc"
    run "$opt" -S "$filename.pp.bc" -o "$filename.new.ll"
    vimdiff "$filename.ll" "$filename.new.ll"
    rm -f "$filename.bc" "$filename.pp.bc" "$filename.new.ll"
done

for filename in *.cpp
do
    echo "[*] $filename ..."
    filename="${filename%.cpp}"

    run "$clang" $cxxflags "$filename.cpp" -o "$filename.bc"
    run "$ikos_pp" -opt=$opt_level "$filename.bc" -o "$filename.pp.bc"
    run "$opt" -S "$filename.pp.bc" -o "$filename.new.ll"
    vimdiff "$filename.ll" "$filename.new.ll"
    rm -f "$filename.bc" "$filename.pp.bc" "$filename.new.ll"
done
